package com.weibo.entry;

import com.weibo.net.AccessToken;
import com.weibo.net.DialogError;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class IweiboActivity extends Activity {
    
    private static final String key = "934738025";
    private static final String password = "ad6c832f1513cc3ba173d5b8225060d6";
    private Button authButton;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.auth);
        
        authButton = (Button) findViewById(R.id.auth_button);
        authButton.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                Weibo weibo = Weibo.getInstance();
                weibo.setupConsumerConfig(key, password);
                weibo.setRedirectUrl("http://www.sina.com");
                weibo.authorize(IweiboActivity.this, new WeiboDialogListener()
                {
                    
                    @Override
                    public void onWeiboException(WeiboException e)
                    {
                        // TODO Auto-generated method stub
                        
                    }
                    
                    @Override
                    public void onError(DialogError e)
                    {
                        // TODO Auto-generated method stub
                        
                    }
                    
                    @Override
                    public void onComplete(Bundle values)
                    {
                        String token = values.getString("access_token");
                        String expires_in = values.getString("expires_in");
                        AccessToken accessToken = new AccessToken(token, password);
                        accessToken.setExpiresIn(expires_in);
                        Weibo.getInstance().setAccessToken(accessToken);
//                        Toast.makeText(IweiboActivity.this, expires_in + "  " + token, 1000).show();
                        Intent intent = new Intent();
                        intent.setClass(IweiboActivity.this, MainScreen.class);
                        startActivity(intent);
                    }
                    
                    @Override
                    public void onCancel()
                    {
                        // TODO Auto-generated method stub
                        
                    }
                });
            }
        });
    }
}